const express = require('express')
const router = express.Router()
const mysql = require("../mysql/config")
let connection = null
const resSend = require('../utils/resSend')

router.post("/add", (req, res) => {
    connection = mysql.createConnection()
    connection.connect()

    let params = [req.user.id,req.body.goods,req.body.money, req.body.coupon]
    // 添加订单
    connection.query(`INSERT INTO order_table (userId,goods,useMoney,coupon) VALUES (?,?,?,?)`, params, (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常！")
        } else {
            resSend(res, 200, '操作成功！')
        }
    })

})

// 获取用户订单
router.get("/getUserOrder", (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.user.id]
    connection.query(`SELECT * FROM order_table WHERE userId=?`, params, (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常！")
        } else {
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 获取所有订单
router.get("/getAllOrder", (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.user.id]
    connection.query(`SELECT * FROM order_table`, params, (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常！")
        } else {
            resSend(res, 200, "操作成功！", result)
        }
    })
})

// 修改优惠券状态
router.post("/update", (req, res) => {
    connection = mysql.createConnection()
    connection.connect()
    let params = [req.body.id]
    // 修改优惠券
    connection.query(`UPDATE useryhq_table SET status=1 WHERE id=?`, params, (err, result) => {
        if (err) {
            resSend(res, 500, "服务异常！")
        } else {
            resSend(res, 200, '操作成功！')
        }
    })
})

module.exports = router